package function;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class Regeocoding {
    public static void main(String [] args) {
        try
        {
            String filePath = ".\\input_zuobiao_1.txt";
            File filename = new File(filePath);
            InputStreamReader reader = new InputStreamReader(new FileInputStream(filename));
            BufferedReader br = new BufferedReader(reader);
            String ip = "";
            ip = br.readLine();
            int i=0;

            File outputFile = new File(".\\output_address_0903.txt");
            outputFile.createNewFile();
            BufferedWriter out = new BufferedWriter(new FileWriter(outputFile));
            while (ip != null ) {
                if (!ip.equals("0,0")) {
                    String urlStr = "http://restapi.amap.com/v3/geocode/regeo?output=xml&location=" + ip +"&key=85db4d7c3cba4d1673ae3afa6e04caf8&radius=100&extensions=all";
                    System.out.println(urlStr);
                    URL url = new URL(urlStr);
                    URLConnection urlConnection = url.openConnection();
                    HttpURLConnection connection = null;
                    if(urlConnection instanceof HttpURLConnection) {
                        connection = (HttpURLConnection) urlConnection;
                        connection.setConnectTimeout(0);
                    }
                    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
                    String urlString = "";
                    String current;
                    while((current = in.readLine()) != null)
                    {
                        urlString += current;
                    }
                    Document doc = null;
                    try {
                        doc = DocumentHelper.parseText(urlString);
                    } catch (DocumentException e) {
                        e.printStackTrace();
                    }
                    Element root = doc.getRootElement();
                    StringBuilder sb = new StringBuilder();
                    getNodes1(root, sb);
                    out.write(sb.toString() + "\r\n");
                    out.flush();

                } else {
                    out.write("###################" + "\r\n");
                    out.flush();
                }
                i++;
                System.out.println(i);
                ip=br.readLine();
            }
            out.close();
        } catch(IOException e) {
            e.printStackTrace();
        }
    }

    public static void getNodes1(Element node, StringBuilder sb){
        if (node.getName().equals("roads") || node.getName().equals("aois") || node.getName().equals("addressComponent")) {
            return;
        }
        sb.append(node.getTextTrim()  + "%");
        List<Element> listElement=node.elements();
        for(Element e:listElement) {
            getNodes1(e, sb);
        }
    }
}

